<?php
namespace app\index\service;
use think\Db;
use think\Cache;
use app\index\model\Nav;
use app\index\model\Type;
use app\index\model\Course;
use app\index\model\KnowUserLog;
use app\index\model\KnowUserRecord;
use app\index\model\KnowUserTraining;



class UserService
{
	public function getUserNav($param = [])
	{
		$user = $param['user'] ?: user();

		$vips = VipService::disposeUserVip($user['id']);

		$navIds = $type = [];

		if ($vips)
		{
			if ( $vips[1])
			{
				$navIds = Nav::getAllDataIds();
			}
			else
			{
				$navIds = array_keys($vips[2]);
			}
		}

		if (!$navIds) return [];

		$data = Nav::getAllData(['nav_id' => $navIds]);

		$typeData = Type::getNavData($navIds);

		$points = Course::getCoursePoints($navIds);

		foreach ($typeData as $key => $v)
		{
			$v['points'] = $points[$v['type_id']] ?: 0;

			$userLogs = KnowUserLog::getUserCount(['type_id' => $v['type_id'],'user' => $user], 'column', 'course.points');

			$v['userPoints'] = $user ? array_sum($userLogs) : 0;

			$type[$v['nav_id']][] = $v;
		}

		foreach ($data as $key => &$v)
		{
			$v['lastLog'] = KnowUserLog::getUserCount(['nav_id'=>$v['nav_id'],'user' => $user], 'value', 'course.type_id', 'know_user_log.updated_time desc');

			$v['type'] = $type[$v['nav_id']];
		}

		return $data;
	}


	public function getUserProgress( $param )
	{
		$user = $param['user'] ?: user();

		if ($param['time'])
		{
			list($param['start'], $param['end']) = returnStartEnd($param['time']);
		}

		$logs = $user ? KnowUserLog::getUserData($param) : [];

		if (!$logs) return [];

		$logs = json_decode(json_encode($logs) ,true);

		$courseIds = array_column($logs['data'], 'course_id');

		$records = $user && $courseIds ? KnowUserRecord::getUserCount(['course_ids'=>$courseIds,'user' => $user], 'column', 'know_user_record.course_id,record') : [];

		$trainings = $user && $courseIds ? KnowUserTraining::getUserTrainIds(['id'=>$courseIds,'user' => $user]) : [];

		foreach($logs['data'] as &$v)
		{
			$v['time'] = Sec2Time($records[$v['course_id']]) ?: 0;

			$v['icon'] = returnCourseType($v['type']);

			$v['date'] = $v['updated_time'];

			$v['grade'] = $v['problem'] = $v['change'] = '-';

			if ($v['type'] == 2 && $trainings[$v['course_id']] !== '')
			{
				$v['grade'] = strip_tags(returnTrain($trainings[$v['course_id']]));
			}
			else if ($v['type'] == 3)
			{
				$v['grade'] = returnCoursePractice(round($v['points']/$v['origin_points']*100));

				$lastLog = KnowUserLog::getLastData(['id'=>$v['course_id'],'log_id'=>$v['id'],'type'=>3,'user' => $user]);

				$v['change'] = ($lastLog !== NULL && $lastLog != $v['points']) ? ($lastLog > $v['points'] ? 0 : 1) : '-';

				$v['problem'] = $v['points'].'/'.$v['origin_points'];
			}
		}

		$logs['nums'] = KnowUserLog::getUserTypeData($param);

		$minutes = KnowUserRecord::getUserCount($param, 'sum', 'record') ?: 0;

		$logs['nums'][1] = Sec2Time($minutes);

		return $logs;
	}


	public function changeUserLog($param)
	{
		$user = user();

		$data = KnowUserLog::getUserDataStatusOne(['id'=>$param['course_id']]);

		if ($data)
		{
			KnowUserLog::updateData([
				'id' => $param['course_id'],
				'status' => $param['status'] !== '' ? $param['status'] : 1 ,
				'points' => $param['points'],
			]);
		}
		else
		{
			KnowUserLog::addData([
				'id' => $param['course_id'],
				'status' => $param['status'] !== '' ? $param['status']: 1 ,
				'points' => $param['points'],
				'origin_points' => $param['points'],
			]);
		}
	}
}